﻿using System;
using System.Windows.Forms;
using Manage.Model;
using Newtonsoft.Json;
using MShare;

namespace Manage.Basic
{
    public partial class FrmLogin : Form
    {
        public FrmLogin()
        {
            InitializeComponent();
        }
        #region 登录
        private void simpleButton1_Click(object sender, EventArgs e)
        {
            Login();
        }
        #endregion
        #region 取消
        private void simpleButton2_Click(object sender, EventArgs e)
        {
            Share.AppClose = true;
            Dispose();
        }
        #endregion

        #region 登录过程
        private void Login()
        {
            try
            {
                Share.AppClose = true;
                if (string.IsNullOrEmpty(tbxUserName.Text))
                {
                    MessageBox.Show("用户名必须填写！");
                    return;
                }
                if (string.IsNullOrEmpty(tbxPassword.Text))
                {
                    MessageBox.Show("密码必须填写！");
                    return;
                }
                string uPass = CryptUtil.Crypt.MD32Crypt(tbxPassword.Text.Trim());
                string uName = tbxUserName.Text.Trim();
                ManagerModel mlm = new ManagerModel();
                mlm.MLoginName = uName;
                mlm.MPassWord = uPass;
                RetModel rm = Share.PostData("51API/Manager/ManagerLogin",  null, JsonConvert.SerializeObject(mlm));
                if (rm.Success)
                {
                    Share.AppClose = false;
                    Share.MoM = JsonConvert.DeserializeObject<ManageOnlineModel>(rm.Data.ToString());
                    Dispose();
                }
                else
                {
                    MessageBox.Show("登录未成功!" + rm.ErrInfo);
                    tbxPassword.Text = "";
                    tbxPassword.Focus();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion
    }
    
}
